package cn.wanho.service.Impl;


import cn.wanho.exapnd.SheetExcel;
import cn.wanho.listener.SheetExcelListener;
import cn.wanho.mapper.ExamSummaryMapper;
import cn.wanho.rvo.*;
import cn.wanho.service.exam.ExamSheetExcel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jingxu
 * @date 2022-8-6
 */
@Service
public class ExamSheetExcelImpl implements ExamSheetExcel {

    @Resource
    private ExamSummaryMapper examSummaryMapper;

    /**
     * @author jingxu
     * @date 2022-8-6
     * 将从Excel中读取的内容填充到数据库中
     */
    @Override
    public ResultVO importExcel(MultipartFile file,Integer stuId) throws IOException {
        Map<String, Object> objectMap = getExcel(file);

        List<SheetExcel> choose = (List<SheetExcel>)objectMap.get("data1");
        List<SheetExcel> blank = (List<SheetExcel>)objectMap.get("data2");
        List<SheetExcel> tf = (List<SheetExcel>)objectMap.get("data3");
        List<SheetExcel> big = (List<SheetExcel>)objectMap.get("data4");

        //将读到的学生作答的选择题存入数据库的考试汇总表中
        for (SheetExcel data : choose) {
            Long subjectId = data.getPaperSubjectId();
            Integer studentId = data.getStudentId();
            String answerContent = data.getStudentAnswerContent();
            examSummaryMapper.insertExamSummary(subjectId, studentId, answerContent);
        }
        for (SheetExcel data : blank) {
            Long subjectId = data.getPaperSubjectId();
            Integer studentId = data.getStudentId();
            String answerContent = data.getStudentAnswerContent();
            examSummaryMapper.insertExamSummary(subjectId,studentId,answerContent);
        }
        for (SheetExcel data : tf) {
            Long subjectId = data.getPaperSubjectId();
            Integer studentId = data.getStudentId();
            String answerContent = data.getStudentAnswerContent();
            examSummaryMapper.insertExamSummary(subjectId,studentId,answerContent);
        }
        for (SheetExcel data : big) {
            Long subjectId = data.getPaperSubjectId();
            Integer studentId = data.getStudentId();
            String answerContent = data.getStudentAnswerContent();
            examSummaryMapper.insertExamSummary(subjectId,studentId,answerContent);
        }

        Map<String, Integer> mapScore = getFraction(stuId);
        if (!mapScore.isEmpty()){
            return    ResultVO.success(ResultEnum.COMMIT_SUCCESS.getResCode(), ResultEnum.COMMIT_SUCCESS.getResMsg(),mapScore);
        }else {
            return  ResultVO.success(ResultEnum.COMMIT_FAILURE.getResCode(), ResultEnum.COMMIT_FAILURE.getResMsg());
        }
    }

    private Map<String,Object> getExcel(MultipartFile file) throws IOException {

        HashMap<String, Object> map = new HashMap<>();

        ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build();

        //创建监听器对象，对于把不同得sheet表要使用不同的监听器对象
        SheetExcelListener listener1 = new SheetExcelListener();
        SheetExcelListener listener2 = new SheetExcelListener();
        SheetExcelListener listener3 = new SheetExcelListener();
        SheetExcelListener listener4 = new SheetExcelListener();

        ReadSheet sheet1 = EasyExcel.readSheet(0).head(SheetExcel.class).registerReadListener(listener1).build();
        ReadSheet sheet2 = EasyExcel.readSheet(1).head(SheetExcel.class).registerReadListener(listener2).build();
        ReadSheet sheet3 = EasyExcel.readSheet(2).head(SheetExcel.class).registerReadListener(listener3).build();
        ReadSheet sheet4 = EasyExcel.readSheet(3).head(SheetExcel.class).registerReadListener(listener4).build();

        excelReader.read(sheet1,sheet2,sheet3,sheet4);

        //关闭Excel读取的流，要不然会一直会读取，对内存，磁盘都有消耗
        excelReader.finish();

        //从监听器中拿到我们从excel中获取的数据
        List<SheetExcel> data1 = listener1.getData();
        List<SheetExcel> data2 = listener2.getData();
        List<SheetExcel> data3 = listener3.getData();
        List<SheetExcel> data4 = listener4.getData();

        //将数据存入map
        map.put("data1",data1);
        map.put("data2",data2);
        map.put("data3",data3);
        map.put("data4",data4);

        return map;
    }

    /**
     * @author jingxu
     * @date 2022-8-6
     * 拿取选择题和填空题等的分数
     */
    private Map<String,Integer> getFraction(Integer studentId){

        int chooseScore = 0;
        int blanksScore = 0;
        int tfScore = 0;

        Map<String,Integer> map = new HashMap<>();

        List<ChooseVO> chooseVOS = examSummaryMapper.checkChooseScore(studentId);
        //遍历该集合每个choose对象就是一个道选择题，将学生答题内容与标准内容比较，一样就答题正确
        for (ChooseVO choose : chooseVOS) {
            if (choose.getChooserAnswer().equalsIgnoreCase(choose.getStudentChooseAnswer())){
                examSummaryMapper.insertSubjectScore(choose.getChooseScore(),studentId,choose.getChooseId());
                chooseScore += choose.getChooseScore();
            }else {
                examSummaryMapper.insertSubjectScore(0,studentId,choose.getChooseId());
            }
        }

        List<BlanksVO> blanksVOS = examSummaryMapper.checkBlanksScore(studentId);
        for (BlanksVO blank : blanksVOS) {
            if (blank.getBlanksAnswer().equals(blank.getStudentBlanksAnswer())){
                examSummaryMapper.insertSubjectScore(blank.getBlanksScore(),studentId,blank.getBlanksId());
                blanksScore += blank.getBlanksScore();
            }else {
                examSummaryMapper.insertSubjectScore(0,studentId,blank.getBlanksId());
            }
        }

        List<TfVO> tfVOS = examSummaryMapper.checkTfScore(studentId);
        for (TfVO tf : tfVOS) {
            if (tf.getTfAnswer().equals(tf.getStudentTfAnswer())){
                examSummaryMapper.insertSubjectScore(tf.getTfScore(),studentId,tf.getTfId());
                tfScore += tf.getTfScore();
            }else {
                examSummaryMapper.insertSubjectScore(0,studentId,tf.getTfId());
            }
        }

        map.put("选择题得分",chooseScore);
        map.put("填空题得分",blanksScore);
        map.put("判断题得分",tfScore);

        return map;
    }

}
